#import "ALElementBarAddButtonCell.h"


#pragma mark Constants

static const CGFloat ALElementBarAddButtonImageHeight = 8.0f;


#pragma mark -
#pragma mark Class variables
static NSColor *ALElementBarBorderColor;
static NSColor *ALElementBarBorderColorSelected;
static NSGradient *ALElementBarBackgroundGradient;
static NSGradient *ALElementBarBackgroundGradientSelected;
static NSGradient *ALElementBarBackgroundGradientPressed;
static NSGradient *ALElementBarBackgroundGradientPressedSelected;


#pragma mark -
@interface ALElementBarAddButtonCell (ALElementBarAddButtonCellPrivateMethods)
-(NSBezierPath *)framePathWithRect_:(NSRect)frameRect;
@end


#pragma mark -
@implementation ALElementBarAddButtonCell

@synthesize selected;
@synthesize useRoundedBottom;


+(void)initialize {
	ALElementBarBorderColor = [[NSColor colorWithCalibratedWhite:0.783 alpha:1.000] retain];
	ALElementBarBorderColorSelected = [[NSColor colorWithCalibratedWhite:0.5f alpha:1.0f] retain];
	NSColor *backgroundTop = [NSColor colorWithCalibratedWhite:1.000 alpha:1.000];
	NSColor *backgroundBottom = [NSColor colorWithCalibratedWhite:0.944 alpha:1.000];
	ALElementBarBackgroundGradient = [[NSGradient alloc] initWithStartingColor:backgroundTop endingColor:backgroundBottom];
	NSColor *topSelected = [NSColor colorWithCalibratedRed:0.864 green:0.895 blue:0.926 alpha:1.000];
	NSColor *bottomSelected = [NSColor colorWithCalibratedRed:0.664 green:0.712 blue:0.833 alpha:1.000];
	ALElementBarBackgroundGradientSelected = [[NSGradient alloc] initWithStartingColor:topSelected endingColor:bottomSelected];
	NSColor *pressedTop = [NSColor colorWithCalibratedWhite:0.6 alpha:1.0f];
	NSColor *pressedBottom = [NSColor colorWithCalibratedWhite:0.8 alpha:1.0f];
	ALElementBarBackgroundGradientPressed = [[NSGradient alloc] initWithStartingColor:pressedTop endingColor:pressedBottom];
	NSColor *pressedSelectedBottom = [NSColor colorWithCalibratedRed:0.664 green:0.695 blue:0.726 alpha:1.000];
	NSColor *pressedSelectedTop = [NSColor colorWithCalibratedRed:0.464 green:0.512 blue:0.633 alpha:1.000];
	ALElementBarBackgroundGradientPressedSelected = [[NSGradient alloc] initWithStartingColor:pressedSelectedTop endingColor:pressedSelectedBottom];
}

-(id)initImageCell:(NSImage *)image {
	if (!(self = [super initImageCell:image]))
		return nil;
	[self setUseRoundedBottom:YES];
	return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self setUseRoundedBottom:YES];
	return self;
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	[self drawBezelWithFrame:cellFrame inView:controlView];
	NSRect imageRect = [self imageRectForBounds:cellFrame];
	[self drawImage:[self image] withFrame:imageRect inView:controlView];
}

-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView {
	NSBezierPath *borderPath = [self framePathWithRect_:NSInsetRect(frame, 0.5f, 0.5f)];
	NSGradient *backgroundGradient = [self selected] ? ALElementBarBackgroundGradientSelected : ALElementBarBackgroundGradient;
	NSGradient *pressedBackgroundGradient = [self selected] ? ALElementBarBackgroundGradientPressedSelected : ALElementBarBackgroundGradientPressed;
	NSGradient *currentGradient = [self isHighlighted] ? pressedBackgroundGradient : backgroundGradient;
	[currentGradient drawInBezierPath:borderPath angle:90.0f];
	NSColor *borderColor = [self selected] ? ALElementBarBorderColorSelected : ALElementBarBorderColor;
	[borderColor setStroke];
	[borderPath stroke];
	
	if ([self showsFirstResponder] && [self focusRingType] != NSFocusRingTypeNone) {
		NSBezierPath *focusPath = [self framePathWithRect_:frame];
		[NSGraphicsContext saveGraphicsState];
		NSSetFocusRingStyle(NSFocusRingOnly);
		[focusPath fill];
		[NSGraphicsContext restoreGraphicsState];		
	}
}

-(NSRect)imageRectForBounds:(NSRect)theRect {
	NSSize imageSize = [[self image] size];
	CGFloat conversion = ALElementBarAddButtonImageHeight / imageSize.height;
	NSSize resultSize = NSMakeSize(imageSize.width * conversion, ALElementBarAddButtonImageHeight);
	CGFloat minx = ceil((NSWidth(theRect) / 2) - (resultSize.width / 2));
	CGFloat miny = ceil(ceil(NSHeight(theRect) / 2) - (resultSize.height / 2));
	return NSMakeRect(minx, miny, resultSize.width, resultSize.height);
}

-(NSBezierPath *)framePathWithRect_:(NSRect)frameRect {
	CGFloat minx = NSMinX(frameRect);
	CGFloat maxx = NSMaxX(frameRect);
	CGFloat miny = NSMinY(frameRect);
	CGFloat maxy = NSMaxY(frameRect);
	CGFloat midy = NSMidY(frameRect);
	CGFloat corner = fmin((NSHeight(frameRect) / 2), (NSWidth(frameRect) / 2));
	NSBezierPath *framePath = [NSBezierPath bezierPath];
	[framePath moveToPoint:NSMakePoint(minx, maxy)];
	[framePath lineToPoint:NSMakePoint(minx, miny)];
	[framePath lineToPoint:NSMakePoint(maxx - corner, miny)];
	[framePath appendBezierPathWithArcFromPoint:NSMakePoint(maxx, miny) toPoint:NSMakePoint(maxx, midy) radius:corner];
	if ([self useRoundedBottom])
		[framePath appendBezierPathWithArcFromPoint:NSMakePoint(maxx, maxy) toPoint:NSMakePoint(maxx - corner, maxy) radius:corner];
	else 
		[framePath lineToPoint:NSMakePoint(maxx, maxy)];

	[framePath closePath];
	return framePath;
}

@end
